package system.student.login.impl;

import system.student.entity.login.LoginRequest;
import system.student.entity.login.LoginResponse;
import system.student.entity.login.LoginType;
import system.student.entity.user.UserInfo;
import system.student.login.LoginStrategy;
import system.student.mapper.UserMapper;
import system.student.util.MapperUtil;


//用户名，密码登录
public class PasswordLoginStrategy implements LoginStrategy {
    private static final UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);

    @Override
    public LoginResponse login(LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String inputPassword = loginRequest.getPassword();
        UserInfo user = userMapper.loginByPassword(username);

        // 用户不存在
        if (user == null) {
            System.out.println("用户不存在");
            return null;
        }

        //查看用户状态
        if (user.getState() == 0){
            System.out.println("账号被锁定");
            return null;
        }

        if (!user.getPassword().equals(inputPassword)) {
            System.out.println("用户名密码错误了");
            return null;
        }

        return system.student.login.impl.LoginStrategy.getLoginResponse(user);
    }

    @Override
    public LoginType getLoginType() {
        return LoginType.Password;
    }
}
